RELEASE=3

# CHANGE IT TO FIT TO YOUR NEEDS
HOME_USER=$(HOME)
PROJECT_ROOT=$(HOME_USER)/myrpm
WWW_DIR=/var/www/html
###############################################

PIT_ROOT=$(PROJECT_ROOT)/pit
PUBLISHER=$(PIT_ROOT)/pushGoogleDownload.py
SCRIPT_PUBLISHER=$(PIT_ROOT)/publish_google.sh
MYRPM_ROOT=$(PROJECT_ROOT)/myrpm
HOME_ROOT=/root
CSU_ROOT=$(PROJECT_ROOT)/csu

#OTHER VAR
MYRPM_BIN=$(MYRPM_ROOT)/myrpm
INTEGRATION_DIR=$(HOME)/integration
RPM_BUILD_DIR=$(HOME_USER)/redhat
VERSION:=$(strip $(shell cat $(PROJECT_ROOT)/VERSION))
SIGN_KEY=RPM-GPG-KEY-jmrenouard
###############################################
MYRPM_TMP_DIR=$(INTEGRATION_DIR)/myrpm
MYRPM_TMP_TGZ=$(INTEGRATION_DIR)/myrpm_tgz
MYRPM_VERSION:=$(strip $(shell cat $(MYRPM_ROOT)/VERSION))
MYRPM_DOC_DIR=$(MYRPM_TMP_DIR)/usr/share/doc/myrpm-$(MYRPM_VERSION).$(RELEASE)
###############################################
CSU_TMP_DIR=$(INTEGRATION_DIR)/csu
CSU_TMP_TGZ=$(INTEGRATION_DIR)/csu_tgz
CSU_VERSION:=$(strip $(shell cat $(CSU_ROOT)/VERSION))

# REPOSITORY RPM EPEL RED HAT/CENTOS 6.X
EPEL_REPO_RPM=http://fr2.rpmfind.net/linux/epel/6/x86_64/epel-release-6-8.noarch.rpm

# GIT INFO
GIT_REPO=https://code.google.com/p/myrpm/
GIT_BRANCH=master
#git_update #csu
all: clean myrpm 

install_env: 
	################################################
	# INSTALL EPEL RPMS
	# INSTALL MAIN DEPENDENCIES
	################################################
	sudo rpm -Fvh $(EPEL_REPO_RPM)
	sudo yum -y install make perl rpm-build perl-Text-Template perl-Archive-Tar	
	sudo yum -y install lftp git createrepo httpd

	sudo service httpd start
	sudo chkconfig httpd on
	sudo service iptables stop
	sudo chkconfig iptables off
	
prepare_all: prepare_myrpm prepare_csu

prepare: 
	################################################
	# CREATING RPM BUILD DIRECTORIES
	# INSTALLING .rpmmacros files
	################################################
	mkdir -p $(RPM_BUILD_DIR)/{BUILD,RPMS/`arch`,RPMS/noarch,SOURCES,SRPMS,SPECS,tmp}
	cp $(PIT_ROOT)/.rpmmacros $(HOME_USER)
	perl -pi -e "s!%HOME%!$(HOME_USER)!g" $(HOME_USER)/.rpmmacros
	sudo cp $(HOME_USER)/.rpmmacros $(HOME_ROOT)
	
prepare_myrpm: prepare
	mkdir -p $(MYRPM_TMP_DIR) $(MYRPM_TMP_TGZ)

prepare_csu: prepare
	mkdir -p $(CSU_TMP_DIR) $(CSU_TMP_TGZ)
	
clean: big_clean
	# SETUP TEMPORARY Factory
	sudo rm -Rf $(INTEGRATION_DIR) $(HOME_USER)/.rpmmacros $(HOME_ROOT)/.rpmmacros

big_clean: clean
	sudo rm -Rf $(RPM_BUILD_DIR)

clean_myrpm: 
	sudo rm -Rf $(MYRPM_TMP_DIR) $(MYRPM_TMP_TGZ)

factor_myrpm:
	################################################
	# CREATING ROOT DIR FOR MYRPM
	# CREATING DOC
	# COPY MYRPM FILES
	################################################
	mkdir -p $(MYRPM_TMP_DIR)/usr/bin
	cp -a $(PIT_ROOT)/build_myrpm.sh $(MYRPM_TMP_DIR)/build.sh
	perl -pi -e "s/%VERSION%/$(MYRPM_VERSION)/g" $(MYRPM_TMP_DIR)/build.sh
	perl -pi -e "s/%RELEASE%/$(RELEASE)/g" $(MYRPM_TMP_DIR)/build.sh

	touch $(MYRPM_TMP_DIR)/pre.sh $(MYRPM_TMP_DIR)/post.sh

	mkdir -p $(MYRPM_DOC_DIR)
	perldoc $(MYRPM_ROOT)/myrpm > $(MYRPM_DOC_DIR)/README
	cp -a $(MYRPM_ROOT)/myrpm  $(MYRPM_TMP_DIR)/usr/bin
	cp -a $(MYRPM_ROOT)/pushRpm $(MYRPM_TMP_DIR)/usr/bin
	cp -a $(MYRPM_ROOT)/rpmFactory $(MYRPM_TMP_DIR)/usr/bin
	cp -a $(MYRPM_ROOT)/xmyrpm $(MYRPM_TMP_DIR)/usr/bin
	cp -a $(MYRPM_ROOT)/rpmMacrosConfig $(MYRPM_TMP_DIR)/usr/bin

	mkdir -p $(MYRPM_TMP_DIR)/usr/local/myrpm
	cp -a $(MYRPM_ROOT)/build_tpl.sh $(MYRPM_TMP_DIR)/usr/local/myrpm

	chmod -R 755 $(MYRPM_TMP_DIR)/usr
	chmod -R 755 $(MYRPM_TMP_DIR)/*.sh
	sudo chown -R root: $(MYRPM_TMP_DIR)/usr 

	mkdir -p $(MYRPM_TMP_TGZ)
	cp -a $(MYRPM_ROOT)/myrpm  $(MYRPM_TMP_TGZ)
	cp -a $(MYRPM_ROOT)/pushRpm $(MYRPM_TMP_TGZ)
	cp -a $(MYRPM_ROOT)/rpmFactory $(MYRPM_TMP_TGZ)
	cp -a $(MYRPM_ROOT)/xmyrpm $(MYRPM_TMP_TGZ)
	cp -a $(MYRPM_ROOT)/rpmMacrosConfig $(MYRPM_TMP_TGZ)
	cp -a $(MYRPM_ROOT)/build_tpl.sh $(MYRPM_TMP_TGZ)
	chmod -R 755 $(MYRPM_TMP_TGZ)

rpm_myrpm:
	################################################
	# MYRPM BUILD
	################################################
	cd $(MYRPM_TMP_DIR);./build.sh

archive_myrpm:
	###################################
	# ARCHIVE MYRPM UTILITIES
	###################################
	rm -f $(PROJECT_ROOT)/release/$(VERSION)/*
	# RPM FILE
	mkdir -p $(PROJECT_ROOT)/release/$(VERSION)
	mv -f $(MYRPM_TMP_DIR)/myrpm*.rpm $(PROJECT_ROOT)/release/$(VERSION)

	# TAR FILE
	cd $(MYRPM_TMP_TGZ);tar cf myrpm-$(MYRPM_VERSION)-$(RELEASE).tar *
	mv -f $(MYRPM_TMP_TGZ)/myrpm-$(MYRPM_VERSION)-$(RELEASE).tar $(PROJECT_ROOT)/release/$(VERSION)

	# TGZ FILE
	cd $(MYRPM_TMP_TGZ);tar czf myrpm-$(MYRPM_VERSION)-$(RELEASE).tgz *
	mv -f $(MYRPM_TMP_TGZ)/myrpm-$(MYRPM_VERSION)-$(RELEASE).tgz $(PROJECT_ROOT)/release/$(VERSION)

	# TBZ FILE
	cd $(MYRPM_TMP_TGZ);tar cjf myrpm-$(MYRPM_VERSION)-$(RELEASE).tbz *
	mv -f $(MYRPM_TMP_TGZ)/myrpm-$(MYRPM_VERSION)-$(RELEASE).tbz $(PROJECT_ROOT)/release/$(VERSION)

	# ZIP FILE
	cd $(MYRPM_TMP_TGZ);zip myrpm-$(MYRPM_VERSION)-$(RELEASE).zip *
	mv -f $(MYRPM_TMP_TGZ)/myrpm-$(MYRPM_VERSION)-$(RELEASE).zip $(PROJECT_ROOT)/release/$(VERSION)

	# GZIP FILE
	cd $(MYRPM_TMP_TGZ);cat myrpm | gzip > myrpm-$(MYRPM_VERSION)-$(RELEASE).gz
	mv -f $(MYRPM_TMP_TGZ)/myrpm-$(MYRPM_VERSION)-$(RELEASE).gz $(PROJECT_ROOT)/release/$(VERSION)

commit_myrpm:
	###################################
	# COMMITING FILES IN GIT REPOSITORY
	# GIT   : $(GIT_REPO) 
	# BRANCH: $(GIT_BRANCH)
	###################################
	cd $(PROJECT_ROOT)/release;git add $(VERSION)
	cd $(PROJECT_ROOT);git commit -a -m "Release MyRPM $(MYRPM_VERSION)-$(RELEASE)"
	cd $(PROJECT_ROOT)/release/$(VERSION);rm -f CHANGELOG_MYRPM
	cd $(PROJECT_ROOT)/release/$(VERSION);git log > CHANGELOG_MYRPM
	cd $(PROJECT_ROOT)/release/$(VERSION);git add CHANGELOG_MYRPM
	cd $(PROJECT_ROOT);git commit -m "Adding change log MyRPM $(MYRPM_VERSION)-$(RELEASE)"
	cd $(PROJECT_ROOT);git push $(GIT_REPO) $(GIT_BRANCH)

rpm_myrpm_sign:	
	perl $(PIT_ROOT)/signRpm.pl $(PROJECT_ROOT)/release/$(VERSION)/*.rpm

publish_myrpm:
	$(SCRIPT_PUBLISHER) $(PUBLISHER) $(PROJECT_ROOT)/release/$(VERSION) 

factor_csu:
	# csu factory installation
	mkdir -p $(CSU_TMP_DIR)/usr/bin
	cp -a $(PIT_ROOT)/build_csu.sh $(CSU_TMP_DIR)/build.sh
	perl -pi -e "s/%VERSION%/$(VERSION)/g" $(CSU_TMP_DIR)/build.sh
	perl -pi -e "s/%RELEASE%/$(RELEASE)/g" $(CSU_TMP_DIR)/build.sh
	touch $(CSU_TMP_DIR)/pre.sh $(CSU_TMP_DIR)/post.sh
	cp -a $(CSU_ROOT)/*  $(CSU_TMP_DIR)/usr/bin
	chmod -R 755 $(CSU_TMP_DIR)/usr
	chmod -R 755 $(CSU_TMP_DIR)/*.sh
	sudo chown -R root: $(CSU_TMP_DIR)/usr

compile_scu:
	# Compile CSU
	cd $(CSU_TMP_DIR);sh build.sh

	# Realize copy in the release directory
	mkdir -p $(PROJECT_ROOT)/release/$(VERSION)
	cp $(CSU_TMP_DIR)/*.rpm $(PROJECT_ROOT)/release/$(VERSION)

git_update:
	cd $(PROJECT_ROOT);git pull $(GIT_REPO) $(GIT_BRANCH)

git_clone:
	cd $(PROJECT_ROOT);git clone $(GIT_REPO) $(GIT_BRANCH)

release_myrpm: myrpm archive_myrpm rpm_myrpm_sign commit_myrpm publish_myrpm
myrpm: clean_myrpm prepare prepare_myrpm factor_myrpm rpm_myrpm  

install_repo: myrpm archive_myrpm rpm_myrpm_sign
	sudo mkdir -p $(WWW_DIR)/repo/generic/{noarch,sources}
	sudo cp $(PROJECT_ROOT)/release/$(VERSION)/*.noarch.rpm $(WWW_DIR)/repo/generic/noarch
	sudo cp $(PROJECT_ROOT)/release/$(VERSION)/*.src.rpm $(WWW_DIR)/repo/generic/sources
	sudo createrepo $(WWW_DIR)/repo/generic/noarch
	sudo createrepo $(WWW_DIR)/repo/generic/sources
	sudo cp $(SIGN_KEY) $(WWW_DIR)/repo

push_repo: install_repo
	sh pushFtp.sh $(WWW_DIR)/repo

change_log: git_update
	cd $(PROJECT_ROOT);git log > CHANGELOG-$(VERSION).$(RELEASE)
	cd $(PROJECT_ROOT);git add CHANGELOG-$(VERSION).$(RELEASE)
	cd $(PROJECT_ROOT);git commit -a -m "Change Log MyRPM $(MYRPM_VERSION)-$(RELEASE)"
#archive_csu   commit_csu

info:
	################################################
	# Global Version	: $(VERSION)
	# Global Realease	: $(RELEASE)
	# Version MyRPM		: $(MYRPM_VERSION)
	# Version CSU		: $(CSU_VERSION)
	################################################
